﻿using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace Stote.UControls
{
    public partial class UCPager : UserControl
    {
        // 委托：没有返回值，pageSize,currentPage,输出参数：totalCount
        public delegate void GetPageData(int pageSize, int currentPage, out int totalCount);
        // 事件：什么时候触发事件。
        public event GetPageData PageChanged = null;
        public UCPager()
        {
            InitializeComponent();
        }

        private void UCPager_Load(object sender, EventArgs e)
        {
            tscbPageSize.SelectedIndex = 0;
        }

        #region 公开的属性

        private int currentPage = 1;
        [Category("杂项"), Description("当前页码"), Browsable(true), DefaultValue(typeof(int), "1")]
        public int CurrentPage
        {
            get { return currentPage; }
            set
            {
                currentPage = value;
                Invalidate();
            }
        }

        private int totalPage = 1;
        [Category("杂项"), Description("总页码，总页码=总记录数/每页显示的条数"), Browsable(true), DefaultValue(typeof(int), "0")]
        public int TotalPage
        {
            get { return totalPage; }
            set
            {
                totalPage = value;
                Invalidate();
            }
        }

        private int totalCount = 0;
        [Category("杂项"), Description("总记录数，总记录数/每页显示的条数=总页码"), Browsable(true), DefaultValue(typeof(int), "0")]
        public int TotalCount
        {
            get { return totalCount; }
            set
            {
                totalCount = value;
                Invalidate();
            }
        }

        private int pageSize = 10;
        [Category("杂项"), Description("每页显示的条数"), Browsable(true), DefaultValue(typeof(int), "10")]
        public int PageSize
        {
            get { return pageSize; }
            set
            {
                pageSize = value;
                Invalidate();
            }
        }

        #endregion

        public void TriggerPageChanged()
        {
            if (PageChanged == null) return;

            PageChanged(pageSize, currentPage, out totalCount); // 触发事件
            // 触发事件之后，事件给你啥？给你totalCount
            // 1。求总页码
            if (totalCount % pageSize == 0)
                totalPage = totalCount / pageSize;
            else
                totalPage = totalCount / pageSize + 1;

            if (totalPage == 0) totalPage = 1;

            // 2。更新tsbTxtCurrentPage和tsbTotalPage
            tsbTxtCurrentPage.Text = currentPage.ToString();
            tslCurrentPageAndTotalPage.Text = $"{currentPage}/{totalPage}";

            // 3。禁用，启用按钮
            if (currentPage == 1)
            {
                tsbFirst.Enabled = false;
                tsbPrev.Enabled = false;
            }
            else
            {
                tsbFirst.Enabled = true;
                tsbPrev.Enabled = true;
            }

            if (currentPage == totalPage)
            {
                tsbNext.Enabled = false;
                tsbLast.Enabled = false;
            }
            else
            {
                tsbNext.Enabled = true;
                tsbLast.Enabled = true;
            }

            if (totalCount == 0)
            {
                tsbFirst.Enabled = false;
                tsbPrev.Enabled = false;
                tsbNext.Enabled = false;
                tsbLast.Enabled = false;
            }
        }

        private void tsbFirst_Click(object sender, EventArgs e)
        {
            currentPage = 1;
            TriggerPageChanged();
        }

        private void tsbPrev_Click(object sender, EventArgs e)
        {
            if (currentPage <= 1)
            {
                return;
            }
            else
            {
                currentPage = currentPage - 1;
                TriggerPageChanged();
            }
        }

        private void tsbNext_Click(object sender, EventArgs e)
        {
            if (currentPage >= totalPage)
            {
                return;
            }
            else
            {
                currentPage = currentPage + 1;
                TriggerPageChanged();
            }
        }

        private void tsbLast_Click(object sender, EventArgs e)
        {
            currentPage = totalPage;
            TriggerPageChanged();
        }

        private void tsbTxtCurrentPage_KeyPress(object sender, KeyPressEventArgs e)
        {
            // 可参考ascii表，
            // (e.KeyChar >= 48 && e.KeyChar <= 57) 表示0-9
            // e.KeyChar == 8       表示退格键 backspace
            // e.KeyChar == 13      表示回车键 enter
            // e.KeyChar == 127     表示删除键 del
            if ((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 8 || e.KeyChar == 13 || e.KeyChar == 127)
            {
                e.Handled = false; // 将 Handled 设置为 false，处理 KeyPress 事件，即按下的键有效。
                if (e.KeyChar == 13)
                {
                    if (!string.IsNullOrEmpty(tsbTxtCurrentPage.Text.Trim()))
                    {
                        currentPage = int.Parse(tsbTxtCurrentPage.Text.Trim());
                    }
                    else
                    {
                        currentPage = 1;
                    }
                    TriggerPageChanged();
                }
            }
            else
            {
                e.Handled = true; // 将 Handled 设置为 true，以取消 KeyPress 事件，即取消按键，按下的键无效
            }
        }

        private void tsbGo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tsbTxtCurrentPage.Text.Trim()))
            {
                currentPage = int.Parse(tsbTxtCurrentPage.Text.Trim());
            }
            else
            {
                currentPage = 1;
            }

            if (currentPage > totalPage)
            {
                currentPage = totalPage;
            }

            if (currentPage < 1)
            {
                currentPage = 1;
            }
            TriggerPageChanged();
        }

        private void tscbPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            pageSize = Convert.ToInt32(tscbPageSize.SelectedItem);
            currentPage = 1;
            if (PageChanged != null)
            {
                TriggerPageChanged();
            }
        }
    }
}
